/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.ruby_string.delayed;

import moved.org.objectweb.asm.AnnotationVisitor;
import moved.org.objectweb.asm.Attribute;
import moved.org.objectweb.asm.Label;
import picounit.features.classloader.mutator.Method;
import picounit.features.classloader.mutator.MethodMutator;

public class DelayedMethodMutator extends MethodMutator {
	private final MethodInstructions instructions;

	public DelayedMethodMutator() {
		this(new DefaultMethodInstructions());
	}

	public DelayedMethodMutator(MethodInstructions instructions) {
		this.instructions = instructions;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// MethodMutator Methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	protected boolean shouldMutate(Method method) {
		instructions.clear();

		return true;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// MethodVisitor Methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public void visitCode() {
		addMethodInstruction(CodeInstruction.INSTANCE);
	}

	@Override
	public AnnotationVisitor visitAnnotation(String name, boolean visible) {
		DelayedAnnotationMutator delayedAnnotationMutator = new DelayedAnnotationMutator();

		addMethodInstruction(new Annotation(name, visible, delayedAnnotationMutator));

		return delayedAnnotationMutator;
	}

	@Override
	public AnnotationVisitor visitAnnotationDefault() {
		DelayedAnnotationMutator delayedAnnotationMutator = new DelayedAnnotationMutator();

		addMethodInstruction(new AnnotationDefault(delayedAnnotationMutator));

		return delayedAnnotationMutator;
	}

	@Override
	public void visitAttribute(Attribute attr) {
		addMethodInstruction(new AttributeInstruction(attr));
	}

	@Override
	public void visitFieldInsn(int opcode, String owner, String name, String desc) {
		addMethodInstruction(new FieldInstruction(opcode, owner, name, desc));
	}

	@Override
	public void visitIincInsn(int var, int increment) {
		addMethodInstruction(new IincInstruction(var, increment));
	}

	@Override
	public void visitInsn(int opcode) {
		addMethodInstruction(new InsnInstruction(opcode));
	}

	@Override
	public void visitIntInsn(int opcode, int operand) {
		addMethodInstruction(new IntInstruction(opcode, operand));
	}

	@Override
	public void visitJumpInsn(int opcode, Label label) {
		addMethodInstruction(new JumpInstruction(opcode, label));
	}

	@Override
	public void visitLabel(Label label) {
		addMethodInstruction(new LabelInstruction(label));
	}

	@Override
	public void visitLdcInsn(Object cst) {
		addMethodInstruction(new LdcInstruction(cst));
	}

	@Override
	public void visitLineNumber(int line, Label start) {
		addMethodInstruction(new LineNumberInstruction(line, start));
	}

	@Override
	public void visitLocalVariable(String name, String desc, String signature, Label start,
		Label end, int index) {

		addMethodInstruction(
			new LocalVariableInstruction(name, desc, signature, start, end, index));
	}

	@Override
	public void visitLookupSwitchInsn(Label dflt, int keys[], Label labels[]) {
		addMethodInstruction(new LookupSwitchInstruction(dflt, keys, labels));
	}

	@Override
	public void visitMaxs(int maxStack, int maxLocals) {
		addMethodInstruction(new MaxsInstruction(maxStack, maxLocals));
	}

	@Override
	public void visitMethodInsn(int opcode, String owner, String name, String desc) {
		addMethodInstruction(new InvokeMethodInstruction(opcode, owner, name, desc));
	}

	@Override
	public void visitMultiANewArrayInsn(String desc, int dims) {
		addMethodInstruction(new MultiANewArrayInstruction(desc, dims));
	}

	@Override
	public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
		DelayedAnnotationMutator delayedAnnotationMutator = new DelayedAnnotationMutator();

		addMethodInstruction(
			new ParameterAnnotation(parameter, desc, visible, delayedAnnotationMutator));

		return delayedAnnotationMutator;
	}

	@Override
	public void visitTableSwitchInsn(int min, int max, Label dflt, Label labels[]) {
		addMethodInstruction(new TableSwitchInstruction(min, max, dflt, labels));
	}

	@Override
	public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
		addMethodInstruction(new TryCatchBlockInstruction(start, end, handler, type));
	}

	@Override
	public void visitTypeInsn(int opcode, String desc) {
		addMethodInstruction(new TypeInstruction(opcode, desc));
	}

	@Override
	public void visitVarInsn(int opcode, int var) {
		addMethodInstruction(new VarInstruction(opcode, var));
	}

	@Override
	public void visitEnd() {
		addMethodInstruction(EndInstruction.INSTANCE);

		instructions.inform();

		instructions.accept(mv);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Protected Methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	protected final DelayedMethodMutator addMethodInstruction(MethodInstruction methodInstruction) {
		instructions.add(methodInstruction);

		return this;
	}
}
